dojo.provide("lotex.report.Cell");
dojo.require("dojo.lang.declare");
dojo.require("lotex.report.AbstractReport");

lotex.report.Cell = function(){
        this.isMerge = false;
        this.beMerge = false;
        this.value = null;
        this.rowSpan = 1;
        this.colSpan = 1;
        this.tagName = "TD";
        this.isData = false;
        this.text = "";
}

dojo.lang.extend(lotex.report.Cell, {
    repVar: /\$\{([^\}]*)\}/i,
    regexp: /\$\{=([^\}]*)\}/i,
    setExpstr: function(v) {
        this.text = "${" + v + "}";
    },

    getExpstr: function() {
        if(!this.hasExpstr()) return null;
        var r = this.text.match(this.regexp)[1];
        return r;
    },

    hasExpstr: function() {
        return this.regexp.test(this.text);
    },

    // TODO getConfig
    getExpScope: function() {
        if (lotex.array.has(this.parent._classnames, '__group')) return 1;
        return 0;
    },

    readConfig: function() {
        var cfg = {};
        var _C = lotex.report.Constants;

        cfg.group = this.getConfigItem("group", _C.SCOPE_ALL, _C.SCOPE_ALL);
        cfg.expand_up_border = this.getConfigItem("expand_up_border");
        var row = this.parent;
        lotex.lang.inhert(cfg, row.getConfig());
        if(cfg.expand_up_border)
            cfg.expand_up_border = row.getConfigItem("expand_up_border");
        return cfg;
    },

    copyProperty: function(tdel) {
        this.text = tdel.innerHTML;
        this.rowSpan = tdel.rowSpan;
        this.colSpan = tdel.colSpan;
        this.tagName = tdel.tagName;
        this.initClassFromDOM(tdel);
    },

    toStr: function() {
        if(this.beMerge) return "";
        if(this.text == null) b();
        //this.text="()()()()";

        var displayText = this.value == null ?
            this.text : (this.repVar.test(this.text) ?
                 this.text.replace(this.repVar, this.value)
        :this.value);


        var s = [];
        s.push('<');
        s.push(this.tagName);
        if(this.rowSpan>1) s.push(' rowspan="'+this.rowSpan+'"');
        if(this.colSpan>1) s.push(' colspan="'+this.colSpan+'"');
        s.push(this.getClassStr());
        s.push('>');
        s.push(displayText);
        s.push('</'+this.tagName+'>');
        return s.join("");
    }
});

dojo.lang.extend(lotex.report.Cell, lotex.report.AbstractReport);